/*!
 * 列表.
 * ecp.qzz.ui.listgrid.js
 * 创建：QZZ
 * 日期：2014-03-01
 */
define(["qzz.panel","qzz.treedataset","qzz.pfilter", "qzz.paged", "qzz.button", "qzz.icheckbox"], function() {
	var qzz = window.qzz;
	qzz.space("ui");
	/**
	 * 列表表格控件.
	 */
	qzz.ui.listgrid = qzz.Class(qzz.ui.panel, {
		/**
		 * 创建函数.
		 */
		_create:function() {
			this.base();
			this.getOption().width = 700;
			this.getOption().autoCollapse = this.getBooleanValue(this.getOption().autoCollapse, true);
			this.getOption().autoWidth = this.getBooleanValue(this.getOption().autoWidth, true);
			this.getOption().autoHeight = this.getBooleanValue(this.getOption().autoHeight, true);
			this.getOption().rowHeight = this.getNumberValue(this.getOption().rowHeight, 36);
			this.getOption().boxRowHeight = this.getNumberValue(this.getOption().boxRowHeight, 32);
					
			this.getOption().colNames = this.getOption().colNames || [];
			this.getOption().colModels = this.getOption().colModels || [];	
			this._colModelJson = {};
			this.getOption().defColWidth = this.getNumberValue(this.getOption().defColWidth, 100);			
			this.getOption().expandWidth = this.getNumberValue(this.getOption().expandWidth, 42);
			
			this.getOption().pageSize = this._getNumberValue(this.getOption().pageSize, 10);
			this.getOption().pageIndex = this._getNumberValue(this.getOption().pageIndex, 1);
			if(this.getOption().pageSizeList == null) {			
			    this.getOption().pageSizeList = [10,25,50,100,200,this.getI18n("qzz.all", "全部")];			    
			}
			
			this.getOption().summary = this.getBooleanValue(this.getOption().summary, 
					this.getBooleanValue(this.getOption().footerrow, false));
			this.getOption().summaryText = this.getOption().summaryText || this.getI18n("qzz.summary", "合计：");
			this.getOption().pageSummaryText = this.getOption().pageSummaryText || this.getI18n("qzz.pageSummary", "合计：");
			
			this.dataSet = new qzz.treedataset();
			this.dataSet.setViewColModels(this.getOption().colModels);
			this.dataSet.isTree = false;
			//添加合计列.
			for(var i = 0, len = this.getOption().colModels.length; i < len; i ++) {
				if(this.getOption().colModels[i].sum === true) {
					this.dataSet.addSumColumn(this.getOption().colModels[i].name);
				}
			}
			//行变更事件.
		    if(this.dataSet.onChanged == null) {
		    	var _this = this;
		    	this.dataSet.onChanged = function(sender) {
		    		_this.logInfo("row changed event");
		    		if(_this.hasbind("rowChanged")) {
		    			_this.trigger("rowChanged", [sender]);
		    		}
		    		if(typeof _this.onRowChanged == "function") {
		    			try {
		    			    _this.onRowChanged(sender);
		    			} catch(e) {
		    				_this.logInfo("onRowChanged event fire error!");
		    			}
		    		}
		    		_this.trigger("onRowChanged", [sender]);
		    	}
		    }
			//小计行对象.
			this._curSum = new qzz.treenode();
			this._curSum.data = {};
			this._hasRender = false;
			if(this.isArray(this.getOption().dataList)) {
				var _this = this;
				this.dataSet.setItemList(this.getOption().dataList, function() {
					_this.dataSet.caculateSum(true);
					if(_this._hasRender) {
						_this.refresh();
					}
				});		
				this.fullCollapse();
				this.getOption().totalCount = this.dataSet.length();
			}
			//是否显示展开层
			this._showExpand = true;
			this._isHeightResize = false;

			//计算表头sheet
			this._parseTitle();			
		    //当前列序号
		    this.colIndex = 1;
		    //区域选择区
		    this.selRect = {"beginRowNode":null,"endRowNode":null,"beginColIndex":1,"endColIndex":1};
		    //上一个选择区域
		    this._selRect = {};		    
		    //排序列表
		    this.sortTables = {};
		    
		    this._colModelIndex = {};
		},
		/**
		 * 渲染表格.
		 */
		_render:function() {
			this.base();
			this.gridTable = this.createElement("TABLE");
			this.thisWindow.appendChild(this.gridTable);
			this.gridTable.style.tableLayout = "fixed";
			this.setStyle(this.gridTable, "treeListGrid");
			this.pageObj = new qzz.ui.paged({"totalCount":this.getOption().totalCount});
			this.pageObj.setPageSizeList(this.getOption().pageSizeList);
			this.pageObj.setPageSize(this.getOption().pageSize);
			var _this = this;
			this.pageObj.bind("onPageChanged", function(pc,ps,pi){
				if(_this._update) {
					_this.getOption().pageCount = pc;
					_this.getOption().pageSize = ps;
					_this.getOption().pageIndex = pi;
					if(_this.hasbind("onPageChanged")) {
						_this.trigger("onPageChanged", [pc, ps, pi]);
						_this.refresh();
						var fieldNames = [];
						var len = _this.getOption().colModels.length;
						for(var i = 0; i < len; i++) {
							var cm = _this.getOption().colModels[i];
							if(cm.editType == "checkBox" || cm.editType == "onoff") {
								fieldNames.push(cm.name);
							}
						}
						_this._refreshCheckAll(fieldNames);
					} else {
						_this.refresh();
					}
				}
			});
			//控制选择事件
	        this.gridTable.onselectstart = this._onselectstart;
	        this._initTitleSheet();
			this._autoWidth();
			
			//复制输入框
	        this.copyDiv = this.createElement("div");
	        this.setStyle(this.copyDiv,"fixDiv");
	        this.copyDiv.style.top = "0px";
	        this.copyDiv.style.left = "0px";
	        this.copyDiv.style.height = "1px";
	        this.copyDiv.style.width = "1px";
	        this.copyInput = this.createElement("textarea");
	        this.setStyle(this.copyInput, "editTextInputer");
	        this.copyInput.style.width="300px";
	        this.copyInput.style.height="300px";
	        this.pasteKeyPress = false;
	        this.copyDiv.appendChild(this.copyInput);
	        this.thisWindow.appendChild(this.copyDiv);
	        //初始化表头.
			
			this.refreshLayout();
			this.refresh();
			this._hasRender = true;
			
			//初始化筛选过滤窗口.
	        if(this.getOption().filter !== false) {
	        	this._asynRun(function(){this._initFilterWindow();},10);
	        }
		},
		/**
		 * 刷新布局.
		 */
		refreshLayout:function() {
			this._createTitle();	
			this._createPager();
			this._autoHeight();
		},
		/**
		 * 窗口变化.
		 */
		doResize:function() {
			//if(this._hasRender) {
				if(this._isHeightResize !== true) {
				    this.refreshLayout();
				    this.refresh();
				    this._autoWidth();
				}
			//}
		},
		/**
		 * 宽度自适应.
		 */
		_autoWidth:function() {
			if(this.getOption().autoWidth) {
				
				this.thisWindow.style.width = "100%";
				if(this.thisWindow.offsetWidth < 650) {
					this.setWidth(650);
					this.thisWindow.style.width = "650px";
				}
			}
		},	
		/**
		 * 自适应高度.
		 */
		_autoHeight:function() {
			if(this.getOption().autoHeight) {
				this._isHeightResize = true;
				this.setHeight(this.gridTable.offsetHeight);
				this._isHeightResize = false;
			}
		},
		setHeight:function(height) {
			if(height != this.getOption().height) {
				this.base(height);
				this._autoWidth();
			}
		},
	    /**
	     * 解析表头.
	     * @param 传入表头字符串
	     */
	    _parseTitle:function() {
	        //清除表头空行	        
	        var titleSheets = [];	        
	        var rlen = 1, clen = 1, colnames = [];
	        if(this.getOption().colNames[0] instanceof Array) {
	        	rlen = this.getOption().colNames.length;
	        	if(this.getOption().colNames[rlen - 1] != null) {
	        		clen = this.getOption().colNames[rlen - 1].length;
	        	} else {
	        		clen = this.getOption().colNames[rlen - 2].length;
	        		rlen --;
	        	}
	        	colnames = this.getOption().colNames;
	        } else {
	        	clen = this.getOption().colNames.length;
	        	colnames = [this.getOption().colNames];
	        }	        
	        var titleRowCount = rlen + 1;
	        var colCount = clen + 1; 
	        //生成表格层次关系
	        for(var i = 1; i <= rlen; i++) {
	        	titleSheets[i] = [];
	        }
	        var colModel = this.getOption().colModels;
	        for(var i = rlen - 1; i >= 0; i--) {
	        	var grow = colnames[i];
	        	var glen = grow.length;
	        	for(var j = 0, k = 0; j < clen; j++) {
	        		var gcol = null;
	        		var cc = grow[k];
        			if(typeof cc == "string") {
        				cc = {"caption":cc};
        				if(i < rlen - 1) {
        					cc.startIndex = j;
        					cc.colSpan = clen;
        				}
        			}
	        		if(k < glen) {
	        			//获取多表头的起始列字段	        			
		        		var stIndex = cc.startIndex;
		        		//如果有起始列字段名
		        		if(stIndex === 0 || stIndex != null) {
		        			//如果名存在则返回列对象
		        			if(stIndex == j) {
		        				gcol = cc;
		        				gcol.isBand = true;
		        				k++;
		        			}
		        			//else 为空则不处理
		        		} else {
		        			//没有则直接返回列,字段列的时候会直接返回当前cell
		        			gcol = cc;
		        			k++;
		        			if((gcol.caption == null || gcol.caption === "") && i < rlen - 1) {
		        				gcol = null;
		        				this.logInfo(this.getI18n("qzz.mergCell", "合并空的单元格"));
		        				//k--;
		        			}		        			
		        		}
	        		}	        		
	        		//如果列对象存在
	        		if(this.isEmpty(gcol)) {
	        			//缓存到titleSheets中
	        			if(i < rlen - 1) {
	        				titleSheets[i + 1][j] = titleSheets[i + 2][j];
	        				var ts = titleSheets[i + 1][j];
	        				if(ts.colIndex == j) {
	        					ts.rowspan ++;
	        				}	        				    
	        				ts.rowIndex = i + 1;
	        			}
	        		} else {
	        			//作为合并列处理，
	        			var ts = {};
	        			titleSheets[i + 1][j] = ts;
	        			var caption = gcol.caption;
	        			if(colModel[j].caption == null) {
	        			    colModel[j].caption = caption;
	        			}
	        			ts.caption = caption;
	        			ts.rowspan = 1;
	        			ts.rowIndex = i+1;
	        			ts.colIndex = j;
	        			ts.isBand = gcol.isBand || false;
	        		    var colspan = gcol.colSpan;
	        			if(this.isNotEmpty(colspan) && parseInt(colspan, 10) > 1) {
	        				ts.colspan = parseInt(colspan, 10);
	        				var colLen = ts.colspan + j;
	        				j++;
	        				for(;j < clen && j < colLen; j++) {
	        					titleSheets[i + 1][j] = ts;
	        				}
	        				j --;
	        			}
	        		    ts = null;
	        		}
	        		gcol = null;
	        	}
	        	grow = null;
	        }
	        this.getOption().titleSheets = titleSheets;
	        this.getOption().titleRowCount = titleRowCount;
	        this.getOption().colCount = colCount;
	    },
	    _initTitleSheet:function() {
	    	this._titleSheets = [];
	    	this._titleSheets.push([]);
	    	this._colModels = [];
	    	for(var i = 1, rlen = this.getOption().titleSheets.length; i < rlen; i++) {
	    		var colSheets = [];
	    		var sheets = this.getOption().titleSheets[i];
	    		var pcell = null, ncell = null;
	    		var k = 0;
	    		for(var j = 0, clen = sheets.length; j < clen; j++) {
	    			var cell = sheets[j];	    			
	    			if(cell != pcell) {
    					ncell = this.extend({}, cell);
    					ncell.colIndex = k;
    				}
	    			var cm = this.getOption().colModels[j];
	    			if(cm.hidden !== true) {
	    				if(ncell != null){
	    					colSheets.push(ncell);
	    				}
	    				if(i == 1) {
	    					this._colModels.push(cm);
	    				}
	    				k++;
	    			} else if(ncell != null) {
	    				ncell.colspan --;
	    			}
	    			pcell = cell;
	    		}
	    		this._titleSheets.push(colSheets);
	    	}
	    },
		/**
		 * 计算显示的列数.
		 */
		_cacShowFields:function() {
			this._colCount = 0;			
			var gw = this.getWidth();
			var sums = 0.0;
			var defW = this.getOption().defColWidth;
			var cms = this._colModels;
			var cwidth = 0, pwidth = 0;
			this._firstFieldName = "";
			this._hideCount = 0;
			var len = cms.length;
			for(var i = 0; i < len && sums < gw; i++) {
				var cm = cms[i];
			    if(this._firstFieldName === "") {
			    	this._firstFieldName = cm.name;
			    }
				pwidth = cwidth;
				cwidth = cm.width;
				if(cwidth == null || cwidth === 0) {
					cwidth = defW;
					cm.width = cwidth;
				}
				if(cm.editType == "checkBox" || cm.editType == "radio" || cm.editType == "onoff") {
					if(cm.isCheck == null && cm.isCheck !== 0) {
						cm.isCheck = true;
					}
					if(cm.unCheck == null && cm.unCheck !== 0) {
						cm.unCheck = false;
					}
				}
				sums += cwidth;
				this._colCount ++;				
			}
			if(sums > gw) {
				sums -= cwidth;
				cwidth = pwidth;
				this._colCount --;
			}
			if(this._colCount == len) {
				this._showExpand = false;
			} else {
				this._showExpand = true;
				if(sums + this.getOption().expandWidth > gw) {
					this._colCount --;
					sums -= cwidth;
					if(sums + this.getOption().expandWidth > gw) {
						this._colCount --;
					}
				}
			}
			this._ysums = gw - sums - 2;
			if(this._showExpand) {
				 this._ysums -= this.getOption().expandWidth
			}
		},
		doShow:function() {
			this.refresh();
			this._autoHeight();
		},
		/**
		 * 创建表头.
		 */
		_createTitle:function() {
			this._cacShowFields();
			var titleSheets = this._titleSheets;
			var colModel = this._colModels;
			var row = null, cell = null, col = null;
			var avgAW = parseInt(this._ysums/this._colCount, 10);
			var _this = this;
			this._pExpandCol = null;
			var cm = null;
			//插入顶层行
			if(this.gridTable.rows.length > 0) {
			    row = this.gridTable.rows[0];
			} else {
				row = this.gridTable.insertRow(0);
			}
			var clen = row.cells.length;
			var j = 0;
			var k = 0;
			if(clen > k) {
				col = row.cells[k];
			} else {
				col = row.insertCell(k);
				clen++;
			}
			this._setHtml(col, "", false);
			col.style.height = "1px";
			col.style.width = "1px";
			col.id = "common_td";
			k++;
			j++;
			var asums = 0;	
			var csum = 0;
			this._hideCount = 0;
			this._colModelIndex = {};
			for(; j <= this._colCount; j++) {
				cm = colModel[j - 1];
				if(clen > k) {
				    col = row.cells[k];
				} else {
					col = row.insertCell(k);
					clen ++;
				}				
				col.style.height = "1px";
				col.id = "common_td";
				csum = cm.width + avgAW;
				asums += avgAW;					
				col.style.width = csum + "px";
				k++;
			}
			//最后一列处理.
			if(k > 1) {
				col.style.width = (csum + this._ysums - asums) + "px";
			}
			//插入展开行
			if(this._showExpand) {
				if(clen > k) {
				    col = row.cells[k];
				} else {
					col = row.insertCell(k);
					clen ++;
				}
				col.style.height = "1px";
				col.id = "common_td";
				col.style.width = this.getOption().expandWidth + "px";
				j++;
				k++;
			}
			//删除多余的列
			while(clen > k) {
				row.deleteCell(clen - 1);
				clen --;
			}
			//插入表头.
			var rlen = this.gridTable.rows.length;			
			for(var i = 1; i < this.getOption().titleRowCount; i++) {
				if(rlen > i) {
				    row = this.gridTable.rows[i];
				} else {
					row = this.gridTable.insertRow(i);
					rlen++;
				}
				
				var clen = row.cells.length;
				var j = 0;
				var k = 0;
				//插入第一隐藏列
				if(clen > k) {
					col = row.cells[k];
				} else {
					col = row.insertCell(k);
					clen++;
				}
				col.style.width = "1px";
				col.id = "common_td";
				this._setHtml(col, "", false);
				col.setAttribute("rowSpan", 1);
				col.setAttribute("colSpan", 1);
				col.style.height = this.getOption().rowHeight + "px";
				k++;
				j++;
				//插入其它列
				for(; j <= this._colCount; j++) {					
					cm = colModel[j - 1];		
					cell = titleSheets[i][j - 1];
					if(cell.rowIndex == i && (cell.colIndex == j - 1)) {
						if(clen > k) {
						    col = row.cells[k];
						} else {
							col = row.insertCell(k);
							clen ++;
						}
						var cspn = this.getNumberValue(cell.colspan, 1);
						if(j + cspn >= this._colCount + 1) {
							//如果超长，则截断
							cspn = this._colCount + 1 - j;
						}
						col.setAttribute("colSpan", cspn);
						col.setAttribute("rowSpan", this.getNumberValue(cell.rowspan, 1));
						col.colIndex = cell.colIndex;
						col.rowIndex = cell.rowIndex;
						var sortTable = this.sortTables[cm.name];
						if(cell.isBand !== true && sortTable != null) {
						    this.thisWindow.appendChild(sortTable);
						}
						this._setHtml(col, cell.caption);
						if((cm.checkAll === true || cm.selectAll === true) && (cm.editType == "checkBox" || cm.editType == "onoff")) {
							if(this.allCheckBox == null){
								this.allCheckBox = {};
							}
							this.allCheckBox[cm.name] = new qzz.ui.input.checkbox();
							var ccb = this.allCheckBox[cm.name];
							//col.appendChild(ccb.getContainer());
							if(col.childNodes.length > 0) {
							    col.insertBefore(ccb.getContainer(), col.childNodes[0]);
							} else {
								col.appendChild(ccb.getContainer());
							}
							ccb.fieldName = cm.name;
							ccb.bind("onClick", function() {
								_this._checkAll(this.fieldName, this.getCheck());
							}, true);
							
							if(cm.editType == "onoff") {
								ccb.onoff = new qzz.ui.button({"move":true, "width":40,"height":22,"common":["onoff_off", "onoff_on"]});
								ccb.hide();
				                var cct = ccb.onoff.getControl()[0];
				                cct.style.margin = "auto";
								if(col.childNodes.length > 0) {
								    col.insertBefore(cct, col.childNodes[0]);
								} else {
									col.appendChild(cct);
								}				
								ccb.onoff.bind("onclick",ccb, function(checkBox, ev){
									checkBox.click();
									this.focus = false;
								}, true);
								
							}
							
							this._refreshCheckAll(cm.name);
						}
						
						col.isBand = cell.isBand;
						if(col.sortDiv == null) {
							col.sortDiv = this.createElement("div");
					        this.setStyle(col.sortDiv, "sortDiv");
						}
						if(cell.isBand !== true) {							
							if(sortTable == null) {
								sortTable = this.createElement("table");
								this.setStyle(sortTable, "sortTable");
						        sortIcon = sortTable.insertRow(0).insertCell(0);
						        this.setStyle(sortIcon, ["sortDefault"]);
						        sortTable.sortIcon = sortIcon;	
						        sortTable.onmousedown = function() {
						        	_this.sortClick = true;
						        }	
						        //设置排序事件
						        sortTable.onmouseup = function(ev) {
						        	var ev = ev || event;
						        	if(_this.getMouseButton(ev) === _this.mouseType.mtLeft) {
							        	//排序状态
							        	this.sortState = this.state;
							        	this.sortIndex = this.colIndex;
							        	if(this.colIndex != _this.colIndex) {
							        		_this.colIndex = this.colIndex;
							        	}
							        	_this.doSortAndFilter(this.sortIndex, this.sortState);							        	
						        	}
						        }
						        this.sortTables[cm.name] = sortTable;
							}
					        sortTable.state = "none";					        				        				        
					        sortTable.style.display="none";
					        col.sortDiv.appendChild(sortTable);
					        //解决ie浏览器下错位的问题
					        if(col.childNodes[0] != null) {
					        	col.insertBefore(col.sortDiv, col.childNodes[0]);
					        } else {
					        	col.appendChild(col.sortDiv);
					        }
						}
						
						this._initTitleEvent(col);
						this.setStyle(col, "treeListTitle");
						k++;			
					}
				}//for j;
				//插入展开行
				if(this._showExpand && i == 1) {
					if(clen > k) {
					    col = row.cells[k];
					} else {
						col = row.insertCell(k);
						clen ++;
					}
					col.style.width = this.getOption().expandWidth + "px";
					this._setHtml(col, "+", false);
					col.setAttribute("rowSpan", this.getOption().titleRowCount - 1);
					this.setStyle(col, "treeListTitle");
					this.addStyle(col, "listbox_expand");
					this._pExpandCol = col;
					this._clearCellEvent(col);
					col.onclick = function() {
						if(this.innerHTML === "-") {
						    _this.fullCollapse();
						    _this._setHtml(this, "+", false);
						} else {
							_this.fullExpand();
							_this._setHtml(this, "-", false);
						}
						_this.refresh();
					}
					k++;
				}
				//删除多余的列
				while(clen > k) {
					row.deleteCell(clen - 1);
					clen --;
				}
			}//for i;
			//创建表头时，不用删除后边多余的行.
		},
		/**
		 * 创建分页.
		 */
		_createPager:function() {
		    var rlen = this.gridTable.rows.length;
		    var row = null
		    if(rlen > this.getOption().titleRowCount) {
		    	row = this.gridTable.rows[rlen - 1];
		    } else {
		    	row = this.gridTable.insertRow(rlen);
		    }
		    var cell = null;
		    if(row.cells.length > 0) {
		    	cell = row.cells[0];
		    } else {
		    	cell = row.insertCell(0);
		    }
		    cell.style.height = this.getOption().rowHeight + "px";		    
		    cell.style.padding = "0px";
		    cell.style.border = "0px solid green";
		    if(row.cells.length > 1) {
		    	cell = row.cells[1];
		    } else {
		    	cell = row.insertCell(1);
		    }
		    var csp = this._colCount - this._hideCount;
		    if(this._showExpand) {
		    	csp++;
		    }
		    cell.setAttribute("colSpan", csp);
		    this.pageObj.setParent(cell);
		    cell.style.paddingRight = "8px";
		    this.setStyle(cell, "gridCell");
		},
		/**
		 * 区域选择样式调整.
		 */
		_mulSelectStyle:function(col, selNode) {
			var cNode = col.parentNode.node;
			var colIndex = col.cellIndex;
			//只读状态
    		if(this.mulCellSelect
	    	    && cNode.scrIndex <= this.selRect.endRowNode.getIndex()
	    			&& cNode.scrIndex >= this.selRect.beginRowNode.getIndex()
	    				&& colIndex <= this.selRect.endColIndex
	    				    && colIndex >= this.selRect.beginColIndex) {
	    		    //区域选择样式处理
	    			if(this.focus) {		    			
	    				if(this.dataSet.getCurNode() === selNode && col.colIndex == this.colIndex) {
	    					if(col.className !== "gridCell") {
		    					this.setStyle(col, "gridCell");			    				
	    					}
	    				} else {
	    					if(col.className != "gridSelRow") {
		    			        this.setStyle(col, "gridSelRow");		                        
	    					}
	                        if(this.hasCopy) {
	                        	this.addStyle(col, "cellCopySate");	                        	
	                        }
                        }
                        if(colIndex == this.selRect.beginColIndex) {
                        	this.addStyle(col, "borderLeftFocus");
                        }
                        if(colIndex == this.selRect.endColIndex) {
                        	this.addStyle(col, "borderRightFocus");
                        }
                        if(colIndex <= this.selRect.endColIndex
    				          && colIndex >= this.selRect.beginColIndex) {
	                        if(cNode.getIndex() == this.selRect.beginRowNode.getIndex()) {
	                        	this.addStyle(col, "borderTopFocus");
	                        }
	                        if(cNode.getIndex() == this.selRect.endRowNode.getIndex()) {
	                        	this.addStyle(col, "borderBottomFocus");	                        	
	                        }
                        }
	                } else {
	                	if(col.className !== "gridNoFocusSelRow") {
                            this.setStyle(col, "gridNoFocusSelRow");
	                	}
                    }
            }
		},
		/**
		 * 刷新一行.
		 */
		_refreshRow:function(rlen, i, bmi, selNode, cn, isSumRow) {
			var colModel = this._colModels;
			if(i < rlen - 1) {
				row = this.gridTable.rows[i];
			} else {
				row = this.gridTable.insertRow(i);
				rlen++;
			}
			var clen = row.cells.length;
			//var cn = this.dataSet.getCurNode();
			row.node = cn;
			var firstCol = null;
			var curcss = "gridReadonly";
			if(bmi % 2 === 0) {
				curcss = "gridCell";
			}
			if(selNode.recNo === cn.recNo) {
				if(this.focus 
					&& this.selRect.beginRowNode != null 
						&& this.selRect.endRowNode != null 
							&& this.selRect.beginRowNode.getIndex() == this.selRect.endRowNode.getIndex()) {
					curcss = "gridSelRow";
				}
			}
			var _this = this;
			var cm = null;
			//显示内容
			var j = 0;
			var k = 0;
			//第一个单元格
			if(clen > k) {
				col = row.cells[k];
			} else {
				col = row.insertCell(k);
				clen++;
			}
			this._setHtml(col, "", false);
			col.style.height = this.getOption().rowHeight + "px";
			col.style.width = "1px";
			col.id = "common_td";
			col.row = row;
			this.setStyle(col, "");
			j++;
			k++;
			for(; j <= this._colCount; j++) {
				cm = colModel[j - 1];
				var ccss = curcss;
				if(k == this.colIndex && ccss == "gridSelRow") {
					//if(cn._expanded) {
					//	ccss += " focusfirst";
					//} else {
					    ccss += " focus";
					//}
				}
				//插入行
				if(cm.hidden !== true) {
					if(k < clen) {
						col = row.cells[k];
					} else {
						col = row.insertCell(k);
						clen++;
					}
					col.id = "gridborder";
					//获得第一列dom
					if(cm.name === this._firstFieldName) {
						firstCol = col;
					}
					//计算合计
					var CData = this.dataSet.getValue(cm.name, cn);
					if(isSumRow !== true && cm.sum === true) {
						if(this._curSum.data[cm.name] == null) {
							this._curSum.data[cm.name] = 0;
						}
						this._curSum.data[cm.name] += parseFloat(CData, 10);
					}
				    var fmstr = this.dataSet.getDisplay(cm.name, CData, cn);
					//针对字段来绑定格式化
					if(this.hasbind("onFormat_" + cm.name)) {
						fmstr = this.trigger("onFormat_" + cm.name, [cm.name, CData, fmstr, cn]);
						if(typeof fmstr == "string") {
							this._setHtml(col, fmstr, false);
						} else if(fmstr.length > 0) {
							col.appendChild(fmstr[0]);
						} else {
							col.appendChild(fmstr);
						}	
					} else {
						if(isSumRow !== true && (cm.editType === "checkBox" || cm.editType === "onoff")) {
							this._setHtml(col, "", false);
							if(col.checkBox == null) {
							    col.checkBox = new qzz.ui.input.checkbox();//this.createElement("input");
							}
							//col.checkBox.type = "checkbox";
							
							col.appendChild(col.checkBox.getContainer());
							var ccw = CData;
							if(ccw === cm.isCheck) {
								col.checkBox.setCheck(true);
							} else {
								col.checkBox.setCheck(false);
							}
							col.checkBox._node = cn;
							col.checkBox._cm = cm;
							col.checkBox.bind("onClick", function() {
								_this._checkBox = true;
								var cval = _this.dataSet.getValue(this._cm.name, this._node);
								if(cval == this._cm.isCheck) {
									cval = this._cm.unCheck;
								} else {
									cval = this._cm.isCheck;
								}
								_this._doCheckBoxClick(this._cm, cval, this._node);
								//执行选择
								var sender = this.getContainer().parentElement;
						    	_this.doSelect(sender, sender.colIndex);
							}, true);
							//col.checkBox.onmouseup = function() {_this.focus = true};	
							
							if(cm.editType === "onoff") {
								if(col.checkBox.onoff == null) {
								    col.checkBox.onoff = new qzz.ui.button({"move":true, "width":40,"height":22,"common":["onoff_off", "onoff_on"]});
								}
								col.checkBox.hide();
								col.checkBox.onoff.setParent(col);
								if(col.checkBox.getCheck() === true) {
									col.checkBox.onoff.setState(1);
								} else {
									col.checkBox.onoff.setState(0);
								}
								var ctn = col.checkBox.onoff.getControl()[0];
		    					if(cm.align == "center") {
		    						ctn.style.marginLeft = "";
		    						ctn.style.margin = "auto";		    						
		    					} else if(cm.align == "right") {
		    						ctn.style.margin = "";
		    						ctn.style.marginLeft = "auto";		    						
		    					} else {
		    						ctn.style.margin = "";
		    						ctn.style.marginLeft = "";
		    					}
								col.checkBox.onoff.bind("onclick", col.checkBox, function(checkBox) {
									_this._checkBox = true;
									var cval = _this.dataSet.getValue(checkBox._cm.name, checkBox._node);
									if(cval == checkBox._cm.isCheck) {
										cval = checkBox._cm.unCheck;
									} else {
										cval = checkBox._cm.isCheck;
									}
									_this._doCheckBoxClick(checkBox._cm, cval, checkBox._node);
									checkBox.setCheck(this._stateValue === 1);
									this.focus = false;
								}, true);
							}
							//col.checkBox.onclick = function() {
							//	var cval = this._cm.isCheck;
							//	if(!this.checked) {
							//		cval = this._cm.unCheck;
							//	}
							//	_this._doCheckBoxClick(this._cm, cval, this._node);
							//}
						} else if(isSumRow !== true && cm.editType == "radio") {
							this._setHtml(col, "", false);
							if(col.radio == null) {
							    col.radio = new qzz.ui.input.checkbox({"type":"radio"});
							}
							
							col.appendChild(col.radio.getContainer());
							var ccw = CData;
							if(ccw === cm.isCheck) {
								col.radio.setCheck(true);
							} else {
								col.radio.setCheck(false);
							}
							col.radio._node = cn;
							col.radio._cm = cm;
							//col.radio.onmousedown = function() {_this._checkBox = true};
							//col.radio.onmouseup = function() {_this.focus = true};
							col.radio.bind("onClick", function() {
								var cval = this._cm.isCheck;
								if(!this.getCheck()) {
									cval = this._cm.unCheck;
								}
								_this._doRadioClick(this._cm, cval, this._node);
								//执行选择
								var sender = this.getContainer().parentElement;
						    	_this.doSelect(sender, sender.colIndex);
							}, true)
						} else {
							if(cm.link === true) {
							    var cellText = col;
		                    	if(cellText.al == null) {
		                    	    cellText.al = this.createElement("span");
		                    	    cellText.al.onmouseup = function() {
		                    	    	var cn = _this.dataSet.getCurNode();
		                    	    	_this.dataSet.beginUpdate();
		                    	    	_this.dataSet.select(this._node);
		    	    	    			this._node.option[this._fieldName].linkClick = true;
		    	    	    			if(_this.trigger("onLinkClick", [_this, this._fieldName, this._node]) !== false) {
		    	    	    			    _this.dataSet.select(cn);
		    	    	    			}
		    	    	    			_this.dataSet.endUpdate(false);
		    	    	    		}                    	    
		                    	}
		                    	if(cn.option[cm.name].linkClick === true) {                    	    	
		                	    	this.setStyle(cellText.al, "link_visited");
		                	    } else {
		                    	    this.setStyle(cellText.al, "link");
		                	    }
		                    	cellText.al._node = cn;
		                    	cellText.al._fieldName = cm.name;
		                    	cellText.al.id = this.dataSet.getIndex();
			    	    		this._setHtml(cellText.al, fmstr);
			    	    		this._setHtml(cellText, "");
			    	    		cellText.appendChild(cellText.al);
			    	    	} else {
							    this._setHtml(col, fmstr, false);
			    	    	}
						}
					} //else hasbind("onFormat_
					col.isBox = false;
					col.colIndex = col.cellIndex;
				    col.style.textAlign = this.getColAlign(cm.name);
				    this.trigger("onCellDraw",[this.dataSet, cm.name, col, cn]);
				    if(this.hasbind("onCellDraw_" + cm.name)) {
					    this.trigger("onCellDraw_" + cm.name,[this.dataSet, cm.name, col, cn]);
				    }
				    this.setStyle(col, ccss);
				    col.style.paddingLeft = "4px";
				    col.style.paddingRight = "4px";
				    //区域选择样式控制.
				    this._mulSelectStyle(col, selNode);
				    this._clearCellEvent(col);
				    if(isSumRow !== true) {
				    	this._initCellEvent(col);
				    }
				    k++;
				}//if hidden
			}//for j
			//插入展开格
			if(this._showExpand) {
				if(clen > k) {
				    col = row.cells[k];
				} else {
					col = row.insertCell(k);
					clen ++;
				}
				col.id = "gridborder";
				col.style.width = this.getOption().expandWidth + "px";
				col.style.textAlign = "center";
				if(cn._expanded === true) {
					this._setHtml(col, "-", false);
				} else {
					this._setHtml(col, "+", false);
				}
				this.setStyle(col, curcss);
				this.addStyle(col, "listbox_expand");
				this._clearCellEvent(col);
				col.onclick = function() {
					var ccn = this.parentNode.node;
					if(ccn._expanded === true) {
					    _this.collapse(this.parentNode, this.parentNode.boxrow);
					    _this._setHtml(this, "+", false);
					    ccn._expanded = false;
					} else {
						_this.expand(this.parentNode, this.parentNode.boxrow);
						_this._setHtml(this, "-", false);
						ccn._expanded = true;
					}
					_this._refreshExpandAll();
				}
				k++;
				j++;
			} else {
				cn._expanded = false;
			}
			//删除多余的列
			while(clen > k) {
				row.deleteCell(clen - 1);
				clen --;
			}
			
			//插入展开面板
			var bi = i + 1;
			if(bi < rlen - 1) {
				brow = this.gridTable.rows[bi];
			} else {
				brow = this.gridTable.insertRow(bi);
				rlen++;
				brow.isBox = true;
			}
			row.boxrow = brow;
			row.firstCol = firstCol;
			brow.mrow = row;
			var csp = this._colCount + 1 - this._hideCount;
			clen = brow.cells.length;
			k = 0;
			//插入隐藏列
			if(brow.cells.length > k) {
		    	col = brow.cells[k];
		    } else {
		    	col = brow.insertCell(k);
		    	clen++;
		    }
			this._setHtml(col, "", false);
			this.setStyle(col, "");
			col.setAttribute("colSpan", 1);
			col.id = "gridborder";
			k++;			
			//首列夸两行
			if(this.getOption().firstSpan === true) {
				if(cn._expanded === true) {
					firstCol.setAttribute("rowSpan", 2);				
				}	
				csp--;
			}
			//插入内容面板
		    if(brow.cells.length > k) {
		    	col = brow.cells[k];
		    } else {
		    	col = brow.insertCell(k);
		    	clen++;		    	
		    }
		    col.id = "gridborder";
		    k++;
		    col.setAttribute("colSpan", csp);
		    this.refreshBox(col, firstCol, cn, selNode, isSumRow);
		    this.setStyle(col, curcss);
		    if(isSumRow !== true) {
			    col.onclick = function() {
			    	_this.doSelect(this.parentNode.mrow.cells[0], _this.colIndex);
			    }
		    } else {
				col.onclick = null;
			}		    
		    col.style.height = this.getOption().rowHeight + "px";		    
		    if(cn._expanded === true) {
		    	brow.style.display = "";		    	
		    } else {
		    	brow.style.display = "none";
		    	firstCol.setAttribute("rowSpan", 1);
		    }
			//删除多余的列
			while(clen > k) {
				row.deleteCell(clen - 1);
				clen --;
			}
			colModel = null;
			
		},
		/**
		 * 刷新面板.
		 */
		refresh:function() {
			var titleSheets = this.getOption().titleSheets;
			var colModel = this._colModels;
			var rlen = this.gridTable.rows.length;
			var row = null, brow = null, col = null;
			var _this = this;
			var rc = this.getOption().titleRowCount + this.getOption().pageSize * 2;
			//计算起始节点，和结束节点，考虑服务端分页.
			var si = 0
			var ilen = this.dataSet.getRecordCount();
			if(this.getOption().pageSize === this.getI18n("qzz.all", "全部") || this.hasbind("onPageChanged")) {
				si = 0
			} else {
				si = (this.getOption().pageIndex - 1) * this.getOption().pageSize;
				ilen = this.getOption().pageSize + si;
				if(ilen > this.dataSet.getRecordCount()) {
					ilen = this.dataSet.getRecordCount();
				}				
			}
			var bmi = 0;
			var i = this.getOption().titleRowCount;
			var selNode = this.dataSet.getCurNode();
			this.dataSet.beginUpdate();
			this.dataSet.gotoChild(si, this.dataSet.rootNode);
			var pexpand = false;
			this._curSum.data = {};
			this.mulCellSelect = (this.selRect.beginColIndex < this.selRect.endColIndex || this.selRect.beginRowNode != null && this.selRect.beginRowNode.getIndex() < this.selRect.endRowNode.getIndex());
			//渲染内容
			for(; i < rc && si < ilen && !this.dataSet.isEof(); i += 2) {
				var cn = this.dataSet.getCurNode();
				this._refreshRow(rlen, i, bmi, selNode, cn);
				pexpand = pexpand || cn._expanded;
				bmi ++;
				si++;
				this.dataSet.next();
			} //for i
			//渲染小计行
			var smyFieldName = "";
			if(this.getOption().pageSummary === true) {				
				for(var cmi = 0, cmlen = this._colModels.length; cmi < cmlen; cmi++) {
					var ccm = this._colModels[cmi];
					if(ccm.hidden !== true) {
						smyFieldName = ccm.name;
						break;
					}
				}
				if(smyFieldName !== "") {
					this._curSum.data[smyFieldName] = this.getOption().pageSummaryText;
				}
				this._refreshRow(rlen, i, bmi, selNode, this._curSum, true);	
				pexpand = pexpand || this._curSum._expanded;
				bmi++;
				si++;
				i += 2;
			}
			//渲染合计行
			if(this.getOption().summary === true) {
				if(smyFieldName == "") {
					for(var cmi = 0, cmlen = this._colModels.length; cmi < cmlen; cmi++) {
						var ccm = this._colModels[cmi];
						if(ccm.hidden !== true) {
							smyFieldName = ccm.name;
							break;
						}
					}
				}
				if(smyFieldName !== "") {
					this.dataSet.setSummary(smyFieldName, this.getOption().summaryText);
				}
				this._refreshRow(rlen, i, bmi, selNode, this.dataSet.rootNode, true);	
				pexpand = pexpand || this.dataSet.rootNode._expanded;
				bmi++;
				si++;
				i += 2;
			}
			this.dataSet.select(selNode);
			this.dataSet.endUpdate(false);
			//删除多余的列
			while(rlen - 1 > i) {
				this.gridTable.deleteRow(rlen - 2);
				rlen --;
			}
			//全展开，全收缩
			if(this._pExpandCol != null) {
				if(pexpand) {
					this._setHtml(this._pExpandCol, "-", false);
				} else {
					this._setHtml(this._pExpandCol, "+", false);
				}
			}
			this.refreshSortState();
			this._autoHeight();
			this.thisWindow.style.overflow = "visible";
	    	//处理区域复制输入框的状态
	    	if(this.copyInput != null && this.dataSet.getRecordCount() > 0 && this.focus) {
	    		this.copyInput.value = this.getCopyData();
		    	var cip = this.copyInput;
        		this._asynRun(function(){
        			if(this.cellMouseDown == false) {
	        			if(cip.offsetHeight > 0) {
	        			    cip.select(); 
	        			    cip.focus();
	        			}
        			}
	        		cip = null;
	        		that = null;
        		}, 10);
	    	}
		},
	    /**
	     * 获取当前选择列字段.
	     * @return 返回当前列字段
	     */
	    getFieldName:function(index) {
	    	 if(index == null) {
	    		 index = this.colIndex;
	    	 }
	    	 var cm = this._colModels[index - 1];
	    	 if(cm != null) {
	    		 return cm.name;
	    	 } else {
	    		 return "";
	    	 }
	    },
	    /**
	     * 刷新排序状态.
	     */
	    refreshSortState:function(fieldName) {
	    	if(fieldName == null) {
	    	    fieldName = this.getFieldName();
	    	}
	    	var sortTable = this.sortTables[fieldName];
	    	if((this.getOption().filter !== false || this.getOption().sort !== false) && 
	    			sortTable != null && this.getColModel(fieldName).filter != false) {
	    		sortTable.colIndex = this.colIndex;
	    		sortTable.style.display = "";			    
			    var isFilter = this.dataSet.getFilterState(fieldName);
			    sortTable.filtered = isFilter;
			    if(this._sortFieldName == fieldName) {
			    	//显示排序状态
			    	if(sortTable.sortState == "none") {
			    		//没有排序.
			    	    if(this.dataSet.filtered && isFilter) {
			    	    	this.setStyle(sortTable.sortIcon, "filter");
			    	    } else {	    	    	    
		        		    this.setStyle(sortTable.sortIcon, "sortDefault");
			    	    }
		        	} else if(sortTable.sortState == "up") {
		        		//向上排序
		        	    if(this.dataSet.filtered && isFilter) {
		        	    	this.setStyle(sortTable.sortIcon, "filterUp");
		        	    } else {		        	    
		        		    this.setStyle(sortTable.sortIcon, "sortUp");
		        	    }
		        	} else if(sortTable.sortState == "down") {
		        		//向下排序
		        	    if(this.dataSet.filtered && isFilter) {
		        	    	this.setStyle(sortTable.sortIcon, "filterDown");
		        	    } else {
		        		    this.setStyle(sortTable.sortIcon, "sortDown");
		        	    }
		        	}
			    	sortTable.state = sortTable.sortState;
			    } else {
			    	//没有排序.
		    	    if(this.dataSet.filtered && isFilter) {
		    	    	this.setStyle(sortTable.sortIcon, "filter");
		    	    } else {	    	    	    
		    		    this.setStyle(sortTable.sortIcon, "sortDefault");
		    	    }
		    	    sortTable.state = "none";
			    }
			}
	    	//隐藏其它列的属性
	    	for(var key in this.sortTables) {
	    		var st = this.sortTables[key];
	    		if(key !== fieldName) {
	    			if(st.filtered != true) {
	    				st.style.display = "none";
    	    		}
	    			if(key != this._sortFieldName && st != null && st.sortState != "none") {
	    				st.sortState = "none";
	    			}
	    		}
	    		st = null;
	    	}
	    },
		/**
		 * 获得列模型.
		 */
		getColModel:function(fieldName) {
			if(this._colModelJson == null) {
				this._colModelJson = {};
			}
			if(this._colModelJson[fieldName] == null) {
				var rcm = null;
				for(var i = 0, len = this._colModels.length; i < len; i++) {
					var tcm = this._colModels[i];
					if(tcm.name === fieldName) {
						this._colModelJson[fieldName] = tcm;
						break;
					}
				}
			}
			return this._colModelJson[fieldName];
		},
		/**
	     * 获取对齐方式.
	     * @param fieldName 字段名称
	     * @return 返回对齐方式
	     */
	    getColAlign:function(fieldName) {
	    	var align = "";
	    	var fo = this.getColModel(fieldName);
	    	var dt = "";
	    	//以配置的为主
	    	if(fo != null) {	    		
	    	    if(fo.align != null) {
	    	    	align = fo.align;
	    	    }
	    	    dt = fo.dataType;
	    	} 
	    	if(align == null || align === ""){
		    	if(dt == null || dt === "") {
		    		dt = "string";
		    	}
	    		//按数据类型自动生成对齐方式
		    	var dataType = dt;//.toLowerCase();	    	
	    		if(dataType === "number" || dataType === "int" || dataType === "float") {
	    			align = "right";
	    		} else if(dataType != "string"){
	    			align = "center";
	    		} else {
	    			align = "left";
	    		}   		
	    	}
	    	return align;
	    },
	    /**
	     * 收缩面板.
	     */
		collapse:function(row, boxrow) {
			boxrow.style.display = "none";
			if(this.getOption().firstSpan === true) {
				if(row.firstCol != null) {
				    row.firstCol.setAttribute("rowSpan", 1);
				}
			}
			this._autoHeight();
		},
		/**
		 * 展开面板.
		 */
		expand:function(row, boxrow){
			boxrow.style.display = "";
			if(this.getOption().firstSpan === true) {
				if(row.firstCol != null) {
				    row.firstCol.setAttribute("rowSpan", 2);
				}
			}
			this._autoHeight();
		},
		/**
		 * 全展开面板.
		 */
		fullExpand:function() {
			this.beginUpdate();
			var cn = this.dataSet.getCurNode();
			this.dataSet.first();
			while(!this.dataSet.isEof()) {
				this.dataSet.getCurNode()._expanded = true;
				this.dataSet.next();
			}
			if(this.getOption().pageSummary === true) {
				this._curSum._expanded = true;
			}
			if(this.getOption().summary === true) {
			    this.dataSet.rootNode._expanded = true;
			}
			this.dataSet.select(cn);
			this.endUpdate(false);
		},
		/**
		 * 全收缩面板.
		 */
		fullCollapse:function(){			
			this.beginUpdate();
			var cn = this.dataSet.getCurNode();
			this.dataSet.first();
			while(!this.dataSet.isEof()) {
				this.dataSet.getCurNode()._expanded = false;
				this.dataSet.next();
			}
			if(this.getOption().pageSummary === true) {
				this._curSum._expanded = false;
			}
			if(this.getOption().summary === true) {
			    this.dataSet.rootNode._expanded = false;
			}
			this.dataSet.select(cn);
			this.endUpdate(false);
		},
		/**
		 * 刷新全展开标志.
		 */
		_refreshExpandAll:function() {
			this.beginUpdate();
			var cn = this.dataSet.getCurNode();
			var pexpand = false;
			this.dataSet.first();
			while(!this.dataSet.isEof()) {
				pexpand = pexpand || this.dataSet.getCurNode()._expanded;
				this.dataSet.next();
			}
			if(this.getOption().pageSummary === true) {
				pexpand = pexpand || this._curSum._expanded;
			}
			if(this.getOption().summary === true) {
				pexpand = pexpand || this.dataSet.rootNode._expanded;
			}
			if(pexpand) {
				this._setHtml(this._pExpandCol, "-", false);
			} else {
				this._setHtml(this._pExpandCol, "+", false);
			}
			this.dataSet.select(cn);
			this.endUpdate(false);
		},
		/**
		 * 全选择标志.
		 */
		_checkAll:function(fieldName, state) {
			this.beginUpdate();
			var cn = this.dataSet.getCurNode();
			this.dataSet.first();
			var cm = this.getColModel(fieldName);
			while(!this.dataSet.isEof()) {
				if(state === true) {
					this.dataSet.setValue(fieldName, cm.isCheck);
				} else {
					this.dataSet.setValue(fieldName, cm.unCheck);
				}
				this.dataSet.next();
			}
			this.dataSet.select(cn);
			this.endUpdate(false);
			this.refresh();
		},
		/**
		 * 刷新全选择标志.
		 */
		_refreshCheckAll:function(fieldNames) {
			if(typeof fieldNames == "string") {
				fieldNames = [fieldNames];
			}
			this.beginUpdate();
			var cn = this.dataSet.getCurNode();
			var unchecks = {}; 
			for(var i = 0, len = fieldNames.length; i < len; i++) {
				unchecks[fieldNames[i]] = false;
			}
			this.dataSet.first();
			while(!this.dataSet.isEof()) {
				var alluncheck = true;
				for(var i = 0, len = fieldNames.length; i < len; i++) {
					var fieldName = fieldNames[i];
					var cm = this.getColModel(fieldName)
					unchecks[fieldName] = unchecks[fieldName] || this.dataSet.getValue(fieldName) == cm.unCheck;
					alluncheck = alluncheck && unchecks[fieldName];
				}
				if(alluncheck) {
					break;
				}
				this.dataSet.next();
			}			
			for(var i = 0, len = fieldNames.length; i < len; i++) {
				var curAll = this.allCheckBox[fieldNames[i]];
				if(curAll != null) {
					if(unchecks[fieldNames[i]] == true || this.dataSet.getRecordCount() == 0) {
						curAll.setCheck(false);
					} else {
						curAll.setCheck(true);
					}
				}
			}
			this.dataSet.select(cn);
			this.endUpdate(false);
		},
		doBlur:function() {
			this.refresh();
		},
		doFocus:function() {
			if(this.cellMouseDown !== true) {
			    this.refresh();
			}
		},
	    /**
	     * 控制选择事件函数.
	     */
	    _onselectstart:function(e) {
	    	var ent = e || window.event;
            var tg = ent.target || ent.srcElement;
            var cancel = false;
            if(!this.shiftKeyDown) {
	            var cancel = tg.tagName == 'INPUT' || tg.tagName == "TEXTAREA";	
//	            if(tg.value === "" || tg.value == null) {
//	            	cancel = false;
//	            } 
            }	
            ent.returnValue = cancel;
            return cancel;
	    },
		doSelect:function(col, colIndex) {
			var cn = col.parentNode.node;
			this.colIndex = colIndex || col.cellIndex;
			this.dataSet.select(cn);
			if(this.selRect.beginRowNode == null) {
				this.selRect.beginRowNode = cn;
				this.selRect.endRowNode = cn;
			}
			this.refresh();
			this.trigger("onSelect",[cn]);
			//单元格单击事件
	    	if(typeof this.onClick == "function") {
	    		try {
	    			//执行选择事件.		    			
	    		    this.onClick(cn, this.getFieldName());
	    		} catch(e) {
	    			this.logInfo("[onClick]ERR:" + e.message);
	    		}
	    	}
	    	this.trigger("onClick",[cn, this.getFieldName()]);
		},
		/**
	     * 双击事件.
	     */
		doCellDBClick:function(col, ev) {
	    	if(typeof this.onDBClick === "function") {
    			try {
    			    this.onDBClick(this);
    			} catch(e) {
    				this.logInfo("[onDBClick] error:" + e.message + e.stack);
    			}
    		}
	    	this.trigger("onDBClick", [this]);
	    },
		/**
		 * 刷新展开格内容.
		 */
		refreshBox:function(col, firstCol, cn, selNode, isSumRow) {
			//return;
			var isExpand = cn._expanded === true;
			var boxTable = col.boxTable;
			var gw = this._getRectWidth();
			if(boxTable == null) {
			    boxTable = this.createElement("TABLE"); 			    
			    //boxTable.style.border = "1px solid green";
			    this.setStyle(boxTable, "treeListGrid");
			    col.appendChild(boxTable);
			    col.boxTable = boxTable;
			    boxTable.style.margin = "8px";
			    boxTable.onselectstart = this._onselectstart;
			}
			var firstColWidth = this.getNumberValue(this.gridTable.rows[0].cells[firstCol.cellIndex].style.width, 0);
//			if(cn._expanded === true) {
//				firstColWidth = this.getNumberValue(this.gridTable.rows[0].cells[firstCol.cellIndex].style.width, 0);
//			}
			boxTable.style.width = (gw - 18 - 8 - firstColWidth) + "px";
			var _this = this;
			var ccount = parseInt((gw - 16)/250, 10);
			var len = this._colModels.length;
			var k = this._colCount;
			var i = 0;
			var rlen = boxTable.rows.length;
			var titleWidth = 150;
			var row = null, col = null;
			var avgw = (gw - ccount * titleWidth - 18)/ccount;
			var sheet = this.getOption().titleSheets;
			while(k < len) {
				if(i < rlen) {
					row = boxTable.rows[i];
				} else {
					row = boxTable.insertRow(i);
					rlen ++;
				}
				var clen = row.cells.length;
				for(var j = 0; j < ccount; j++) {
					var xi = 2 * j;
					if(xi < clen) {
						col = row.cells[xi];
					} else {
						col = row.insertCell(xi);
						clen ++;
					}
					col.style.height = this.getOption().boxRowHeight + "px";
					col.style.width = titleWidth + "px";
					col.style.textAlign = "right";
					col.style.fontWeight = "bold";
					//this.setStyle(col, "gridCell");
					//过滤掉隐藏列.
					cm = this._colModels[k];
					while(k < len && cm != null && cm.hidden === true ) {
						k++;
						cm = this._colModels[k];
					}
					if(k < len) {						
						var ri = this.getOption().titleRowCount - 1;
						var psheet = sheet[ri][k];
						var cap = psheet.caption;
						ri--;
						while(ri >= 1) {
							if(psheet.rowIndex != sheet[ri][k].rowIndex) {
							    cap = sheet[ri][k].caption + "/" + cap;
							    psheet = sheet[ri][k];
							}
							ri--;
						}
						this._setHtml(col, cap + ":", false);				
					} else {
						this._setHtml(col, "", false);
						col.colIndex = -1;
						this.setStyle(col, "");
					}
					xi++;
					if(xi < clen) {
						col = row.cells[xi];
					} else {
						col = row.insertCell(xi);
						clen ++;
					}
					col.style.height = this.getOption().boxRowHeight + "px";
					col.style.width = avgw + "px";
					col.isBox = true;
					col.node = cn;
					if(k < len) {					
						this._setHtml(col, "", false);
						if(isSumRow !== true && (cm.editType === "checkBox" || cm.editType === "onoff")) {
							if(col.checkBox == null) {
							    col.checkBox = new qzz.ui.input.checkbox(); 
							}
							//col.checkBox.type = "checkbox";
							col.appendChild(col.checkBox.getContainer());
							var ccw = this.dataSet.getValue(cm.name);
							if(ccw === cm.isCheck) {
								col.checkBox.setCheck(true);
							} else {
								col.checkBox.setCheck(false);
							}
							col.checkBox._node = cn;
							col.checkBox._cm = cm;
							col.checkBox.bind("onClick", function() {
								_this._checkBox = true;
								var cval = _this.dataSet.getValue(this._cm.name, this._node);
								if(cval == this._cm.isCheck) {
									cval = this._cm.unCheck;
								} else {
									cval = this._cm.isCheck;
								}
								_this._doCheckBoxClick(this._cm, cval, this._node);
								//执行选择
								var sender = this.getContainer().parentElement;
						    	_this.doSelect(sender, sender.colIndex);
							}, true);
							//col.checkBox.onmouseup = function() {_this.focus = true;}
							
							if(cm.editType === "onoff") {
								if(col.checkBox.onoff == null) {
								    col.checkBox.onoff = new qzz.ui.button({"move":true, "width":40,"height":22,"common":["onoff_off", "onoff_on"]});
								}
								col.checkBox.hide();
								col.checkBox.onoff.setParent(col);
								if(col.checkBox.getCheck() === true) {
									col.checkBox.onoff.setState(1);
								} else {
									col.checkBox.onoff.setState(0);
								}
								var ctn = col.checkBox.onoff.getControl()[0];
		    					if(cm.align == "center") {
		    						ctn.style.marginLeft = "";
		    						ctn.style.margin = "auto";		    						
		    					} else if(cm.align == "right") {
		    						ctn.style.margin = "";
		    						ctn.style.marginLeft = "auto";		    						
		    					} else {
		    						ctn.style.margin = "";
		    						ctn.style.marginLeft = "";
		    					}
								col.checkBox.onoff.bind("onclick", col.checkBox, function(checkBox) {
									_this._checkBox = true;
									var cval = _this.dataSet.getValue(checkBox._cm.name, checkBox._node);
									if(cval == checkBox._cm.isCheck) {
										cval = checkBox._cm.unCheck;
									} else {
										cval = checkBox._cm.isCheck;
									}
									_this._doCheckBoxClick(checkBox._cm, cval, checkBox._node);
									checkBox.setCheck(this._stateValue === 1);
									this.focus = false;
								}, true);
							}
//							col.checkBox.onclick = function() {
//								var cval = this._cm.isCheck;
//								if(!this.checked) {
//									cval = this._cm.unCheck;
//								}
//								_this._doCheckBoxClick(this._cm, cval, this._node);
//							}
						} else if(isSumRow !== true && cm.editType == "radio") {
							this._setHtml(col, "", false);
							if(col.radio == null) {
							    col.radio = new qzz.ui.input.checkbox({"type":"radio"});
							}
							col.appendChild(col.radio.getContainer());
							var ccw = this.dataSet.getValue(cm.name);
							if(ccw === cm.isCheck) {
								col.radio.setCheck(true);
							} else {
								col.radio.setCheck(false);
							}
							col.radio._node = cn;
							col.radio._cm = cm;
							//col.radio.onmousedown = function() {_this._checkBox = true};
							//col.radio.onmouseup = function() {_this.focus = true;}
							col.radio.bind("onClick", function() {
								var cval = this._cm.isCheck;
								if(!this.getCheck()) {
									cval = this._cm.unCheck;
								}
								_this._doRadioClick(this._cm, cval, this._node);
								//执行选择
								var sender = this.getContainer().parentElement;
						    	_this.doSelect(sender, sender.colIndex);
							}, true);
						} else {
							var fmstr = this.dataSet.getDisplay(cm.name, null, cn);
							if(cm.link === true) {
							    var cellText = col;
		                    	if(cellText.al == null) {
		                    	    cellText.al = this.createElement("span");
		                    	    cellText.al.onmouseup = function() {
		                    	    	var cn = _this.dataSet.getCurNode();
		                    	    	_this.dataSet.beginUpdate();
		                    	    	_this.dataSet.select(this._node);
		    	    	    			this._node.option[this._fieldName].linkClick = true;
		    	    	    			if(_this.trigger("onLinkClick", [_this, this._fieldName, this._node]) !== false) {
		    	    	    			    _this.dataSet.select(cn);
		    	    	    			}
		    	    	    			_this.dataSet.endUpdate(false);
		    	    	    		}                    	    
		                    	}
		                    	if(cn.option[cm.name].linkClick === true) {                    	    	
		                	    	this.setStyle(cellText.al, "link_visited");
		                	    } else {
		                    	    this.setStyle(cellText.al, "link");
		                	    }
		                    	cellText.al._node = cn;
		                    	cellText.al._fieldName = cm.name;
		                    	cellText.al.id = this.dataSet.getIndex();
			    	    		this._setHtml(cellText.al, fmstr);
			    	    		this._setHtml(cellText, "");
			    	    		cellText.appendChild(cellText.al);
			    	    	} else {
							    this._setHtml(col, fmstr, false);	
			    	    	}
						}
						col.style.paddingLeft = "8px";
						col.colIndex = k + 1;
						if(this.colIndex == k + 1 && cn.recNo == selNode.recNo 
								|| k + 1 >= this.selRect.beginColIndex && k + 1 <= this.selRect.endColIndex 
								    && this.selRect.beginRowNode != null && cn.getIndex() >= this.selRect.beginRowNode.getIndex() && cn.getIndex() <= this.selRect.endRowNode.getIndex()) {
							if(this.focus) {
							    this.setStyle(col, "gridSelRow focus");
							} else {
								this.setStyle(col, "gridNoFocusSelRow");
								
							}
						} else {
							this.setStyle(col, "");
						}
						if(isSumRow !== true) {
						    this._initCellEvent(col);
						} else {
							this._clearCellEvent(col);
						}
					} else {
						this._setHtml(col, "", false);
						col.colIndex = -1;
						this.setStyle(col, "");
					}	
					//this.setStyle(col, "gridCell");
					k++;					
				}//for j
				//删除多余的列.
				while(clen > 2 * j) {
					row.deleteCell(clen - 1);
					clen--;
				}
				i++;				
			}//while
			//删除多余的列
			while(rlen > i) {
				boxTable.deleteRow(rlen - 1);
				rlen --;
			}
		},
		/**
		 * 单选框点击事件.
		 * @name _onCommonCheckBoxClick
		 * @ignore
		 */
		_doRadioClick:function(cm, checked, cn) {
			var fieldName = cm.name;
			if(this._radios == null) {
				this._radios = {};
			}
			if(checked == cm.isCheck) {
				//把当前设置为选择，
				var ov = this.dataSet.getValue(fieldName, cn);
				if(ov !== checked) {
					this.dataSet.setValue(fieldName, checked, cn);
					//把上一个选择节点设置为空
					if(this._radios[fieldName] == null) {
						this.dataSet.beginUpdate();
						this.dataSet.first();
						while(!this.dataSet.isEof()) {
							if(this.dataSet.getValue(fieldName) === cm.isCheck) {
								this._radios[fieldName] = this.dataSet.getCurNode();
								break;
							}
							this.dataSet.next(true);
						}
						this.dataSet.select(cn);
						this.dataSet.endUpdate();
					}
					if(this._radios[fieldName] != null) {
						this.dataSet.setValue(fieldName, cm.unCheck, this._radios[fieldName]);
					}
					this._radios[fieldName] = cn;
				}
				if(typeof this.onCheckChanged === "function") {
					try {
						this.onCheckChanged(cn, checked, fieldName);		    						
					} catch(e) {
						this.logInfo("[onCheckChanged] error:" + e.message + e.stack);
					}
				}
				this.trigger("onCheckChanged", [cn, checked, fieldName, "@MUL"]);
				this.trigger("onSelectRow",[cn, checked, fieldName, "@MUL"]);
			}			
		},
		/**
		 * 复选框点击事件.
		 * @name _onCommonCheckBoxClick
		 * @ignore
		 */
		_doCheckBoxClick:function(colModel, checked, cn) {
			var grid = this;
			var fieldName = colModel.name;
			var oldValue = grid.dataSet.getValue(fieldName, cn);
			if(oldValue != checked) {
			    grid.dataSet.setValue(fieldName, checked, cn);
			}
			if(colModel.checkAll === true) {
				if(checked == colModel.unCheck) {
					this.allCheckBox[fieldName].setCheck(false);				
				} else {					
			        this._refreshCheckAll(fieldName);
				}
			}
			//复选择选择事件.
			if(typeof this.onCheckChanged === "function") {
				try {
					this.onCheckChanged(cn, checked, fieldName);		    						
				} catch(e) {
					this.logInfo("[onCheckChanged] error:" + e.message + e.stack);
				}
			}
			this.trigger("onCheckChanged", [cn, checked, fieldName, "@MUL"]);
			this.trigger("onSelectRow",[cn, checked, fieldName, "@MUL"]);
		},
		/**
		 * 初始化值.
		 */
		value:function(datas, callBack) {
			if(datas != null) {
				this.getOption().dataList = datas;
				if(this.isArray(this.getOption().dataList)) {
					var _this = this;
					this.dataSet.setItemList(this.getOption().dataList, function(){
						_this.dataSet.first();
						_this.fullCollapse();
						if(_this.hasbind("onPageChanged") === false) {
							_this.getOption().totalCount = _this.dataSet.getRecordCount();
							_this.pageObj.setTotalCount(_this.getOption().totalCount);
						}						
						_this.refresh();
						//初始化后事件
			    		if(typeof _this.onAfterLoad == "function") {
			    			try {
			    				_this.onAfterLoad();
			    				
			    			} catch(e) {
			    				_this.logInfo("[onAfterLoad] error:" + e.message + e.stack);
			    			}
			    		}
			    		_this.trigger("gridComplete",[]);
			    		if(typeof callBack === "function") {
			    			callBack.apply(_this);
			    		}
					});					
				}
			} else {
				return this.getOption().dataList;
			}
		},
		getWidth:function() {
			var re = 0;
			if(this.getOption().autoWidth) {
				re = this.thisWindow.offsetWidth;
			}
			if(re === 0) {		
			    re = this.base();
			}
			return re;
		},
	    _initTitleEvent:function(col) {
	    	var _this = this;
	    	col.onmouseover = function(ev) {
				var ev = ev || window.event;
				_this.doTitleMouseOver(this, ev);
			}
	    	col.onmousemove = function(ev) {
				var ev = ev || window.event;
				_this.doTitleMouseMove(this, ev);
			}
	    	col.onmousedown = function(ev) {
				var ev = ev || window.event;
				_this.doTitleMouseDown(this, ev);
			}
	    	col.onmouseup = function(ev) {
				var ev = ev || window.event;
				_this.doTitleMouseUp(this, ev);
			}
	    	col.ondblclick = function(ev) {
	    		var ev = ev || window.event;
	    		_this.doTitleDBClick(this, ev);
	    	}
	    },
	    _clearCellEvent:function(col) {
	    	col.onclick = null;
	    	col.ondblclick = null;
		    col.onmouseup = null;
		    col.onmousedown = null;
		    col.onmouseover = null;
		    col.onmousemove = null;
	    },
	    _initCellEvent:function(col) {
	    	var _this = this;
	    	col.onmouseover = function(ev) {
				var ev = ev || window.event;
				_this.doCellMouseOver(this, ev);
			}
//	    	col.onmousemove = function(ev) {
//				var ev = ev || window.event;
//				_this.doCellMouseMove(this, ev);
//			}
	    	col.onmousedown = function(ev) {
				var ev = ev || window.event;
				_this.doCellMouseDown(this, ev);
			}
	    	col.onmouseup = function(ev) {
				var ev = ev || window.event;
				_this.doCellMouseUp(this, ev);
			}
	    	col.ondblclick = function(ev) {
	    		var ev = ev || window.event;
	    		_this.doCellDBClick(this, ev);
	    	}
	    },
	    /**
         * 标题双击事件
         * @name doTitleDBClick
         */
	    doTitleDBClick:function(sender, ev) {
	    	
	    },
	    /**
	     * 获取原列宽度.
	     * @param titleCol 传入的列对象
	     */
	    _getColWidth:function(titleCol) {
	    	var colsWidth = [];
	    	var colSpan = parseInt(titleCol.getAttribute("colSpan"), 10);
	    	if(colSpan > 0) {	 
			    //原来多列，隐藏后就不对了
			    var k = 0;
	        	for(var i = 0; i < colSpan; i++) {
					var tc = this.gridTable.rows[0].cells[titleCol.colIndex + i + k + 1];
					while(tc.style.display == "none") {
						k++;
						tc = this.gridTable.rows[0].cells[titleCol.colIndex + i + k + 1];
					}
	        		colsWidth[i] = this._getNumberValue(tc.style.width, 0);
	        	}
	        }
	    	return colsWidth;
	    },
	    /**
	     * 执行标题鼠标按下事件.
	     * @param sender 控件对象
	     * @param ev 事件对象
	     */
	    doTitleMouseDown:function(sender, ev) {
	    	if(this.getMouseButton(ev) == this.mouseType.mtLeft && !this.sortClick ) {
		    	this.titleMouseDown = true;
		        this.curTitleCell = sender;
		        this.curTitleCellWidth = this._getColWidth(sender);	
		        var cspn = this._getNumberValue(sender.getAttribute("colspan"), 1);
		        this.curOptionWidth = [];
		        for(var i = sender.colIndex; i < sender.colIndex + cspn; i++) {
		        	this.curOptionWidth.push(this._colModels[i].width);
		        }
		        if(sender.colIndex + cspn + 1 <= this._colCount) {
			        this.nextTitleCell = this.gridTable.rows[0].cells[sender.colIndex + cspn + 1];
			        this.nextTitleCellWidth = this._getNumberValue(this.nextTitleCell.style.width);
			        this.nextOptionWidth = this._colModels[this.nextTitleCell.cellIndex - 1].width;
		        } else {
		        	this.nextTitleCell = null;
			        this.nextTitleCellWidth = 0;
			        this.nextOptionWidth = 0;
		        }
		        this.curMouseLeft = this.mouseLeft;
		        this.selMulCol = false;
	        }
	    	//this.post();
    		if(!this.focus) {
    			this.setFocus();
    		}
	    },
        /**
         * 执行标题鼠标弹起事件.
	     * @param sender 控件对象
	     * @param ev 事件对象
         */	    
	    doTitleMouseUp:function(sender, ev) {
	    	if(this.getMouseButton(ev) == this.mouseType.mtLeft && !this.sortClick && !this.resizeState && !this.titleDragState && !this.selMulCol) {
		    	//选择列
	    		if(ev.ctrlKey && sender.cellIndex == 1) {
	    			this.colIndex = 1;
	    			this.selRect.beginColIndex = 1;
	    			this.selRect.endColIndex = this._colModels.length;
	    		} else {
			    	this.selRect.beginColIndex = sender.colIndex + 1;
			    	this.selRect.endColIndex = sender.colIndex + parseInt(sender.getAttribute("colSpan"), 10);
			    	this.colIndex = sender.colIndex + 1;		    			
	    		}
	    		if(this.dataSet.getRecordCount() > 0) {
		    		this.dataSet.beginUpdate();
		    		this.dataSet.last();
		    		this.selRect.endRowNode = this.dataSet.getCurNode();
		    		this.dataSet.first();
		    		this.selRect.beginRowNode = this.dataSet.getCurNode();	    		
		    		this.dataSet.endUpdate(false);
	    		}	    		
	    		//点表头时直接进入焦点.
	    		if(!this.focus) {
	    			this.setFocus();
	    		}
	    		this.refresh();
	    	}
	    	this.trigger("onHeaderClick",true);
	    	this.selMulCol = false; 
	    },
	    /**
	     * 执行标题鼠标移动事件.
	     * @param sender 控件对象
	     * @param ev 事件对象
	     */
	    doTitleMouseMove:function(sender, ev) {
	    	if(!this.titleMouseDown) {   
	    		var sleft = 0;
		        var oRect = sender.getBoundingClientRect();
			    var disX = ev.clientX + sleft;
		        var w = sender.offsetWidth;
		        var vr = w - (ev.clientX - oRect.left);
		        var cspn = this.getNumberValue(sender.getAttribute("colspan"), 1);
		        var cIndex = sender.colIndex;
		        if (vr < 10 && cIndex + cspn < this._colCount) {
		        	this.resizeState = true;
		        	this.titleDragState = true;
		        	if(this.getBrowser("safari")) {
		        		this.thisWindow.style.cursor = "e-resize";
		        	} else {
		        		this.thisWindow.style.cursor = "col-resize";		        		
		        	}
		        } else {
		        	this.titleDragState = false;
		        	this.resizeState = false;
		        	this.thisWindow.style.cursor = "";
		        }
		        this.mouseLeft = ev.clientX;
		        sleft = null;
		        oRect = null;
		        disX = null;
		        w = null;
		        vr = null;
	        }
	    },
	    /**
	     * 执行标题鼠标移过事件.
	     * @param sender 控件对象
	     * @param ev 事件对象
	     */
	    doTitleMouseOver:function(sender, ev) {
	    	if(this.titleMouseDown && !this.titleDragState) {
	    		this.selMulCol = true;
	    		if(this.curTitleCell.colIndex <= sender.colIndex) {
	    			this.selRect.beginColIndex = this.curTitleCell.colIndex + 1;
	    			this.selRect.endColIndex = sender.colIndex + this.getNumberValue(sender.getAttribute("colSpan"), 1);
	    		} else {
	    			this.selRect.beginColIndex = sender.colIndex + 1;
	    			this.selRect.endColIndex = this.curTitleCell.colIndex + 1;
	    		}
	    		if(this.selRect.beginColIndex < 1) {
	    			this.selRect.beginColIndex = 1;
	    		}
	    		this.colIndex = this.curTitleCell.colIndex + 1;
	    		if(this.dataSet.getRecordCount() > 0) {
		    		this.dataSet.beginUpdate();
		    		this.dataSet.last();
		    		this.selRect.endRowNode = this.dataSet.getCurNode();
		    		this.dataSet.first();
		    		this.selRect.beginRowNode = this.dataSet.getCurNode();	    		
		    		this.dataSet.endUpdate(false);
	    		}
	    		this.refresh();
	    	}
	    },
	    /**
	     * 执行单元格鼠标按下事件.
	     * @param sender 控件对象
	     * @param ev 事件对象
	     */
	    doCellMouseDown:function(sender, ev) {
	    	if(this.getMouseButton(ev) == this.mouseType.mtLeft && this._checkBox !== true) {
	    		this.cellMouseDown = true;		    		
		    	if(this.focus === false) {
		    		this.setFocus();
		    	}
		    	var cnode = sender.parentNode.node;
		    	if(sender.isBox === true) {
		    		cnode = sender.node;
		    	}
		    	var snode = null;
		    	if(this.curCell != null && this.curCell.parentNode != null) {
		    		snode = this.curCell.parentNode.node;
		    		if(this.curCell.isBox === true) {
			    		snode = this.curCell.node;
			    	}
		    	}
	    		if(ev.shiftKey) {
	    			if(sender.cellIndex >= this.curCell.cellIndex) {
	    				this.selRect.beginColIndex = this.curCell.colIndex;
	    			    this.selRect.endColIndex = sender.colIndex;
	    			} else if(sender.cellIndex < this.curCell.colIndex) {
	    				this.selRect.endColIndex = this.curCell.colIndex;
	    				this.selRect.beginColIndex = sender.colIndex;
	    			}
	    			snode = this.dataSet.getCurNode();
	    			if(cnode.getIndex() >= snode.getIndex()) {
	    				this.selRect.beginRowNode = snode;
	    				this.selRect.endRowNode = cnode;
	    			} else if(cnode.getIndex() < snode.getIndex()) {
	    				this.selRect.beginRowNode = cnode;
	    				this.selRect.endRowNode = snode;
	    			}
	    			this.refresh();
	    		} else {
	    			this.curCell = sender;
			    	if(this.curCell.colIndex == 0) {
			    		this.selARow = true;
			    	} else {
			    		this.curColIndex = sender.colIndex;
			    	}
			    	this._selRect = this.selRect;
			    	this.selRect = {"beginColIndex":sender.colIndex, "endColIndex":sender.colIndex, 
			    			"beginRowNode":cnode,"endRowNode":cnode};
			    	//执行选择
			    	this.doSelect(sender, sender.colIndex);		
	    		}
	    	}
	    	//this.logInfo("mouse down");
	    },
	    doCellMouseUp:function(sender, ev) {
	    	if(this.getMouseButton(ev) == this.mouseType.mtLeft) {
	    	}
	    },
	    /**
	     * 执行单元格鼠标移过事件.
	     * @param sender 控件对象
	     * @param ev 事件对象
	     */
	    doCellMouseOver:function(sender, ev) {
	    	if(this._enabled != false && this.cellMouseDown === true && this.curCell != null) {
	    		if(this.selARow || ev.ctrlKey) {
	    			this.selRect.beginColIndex = 1;
	    			this.selRect.endColIndex = this._colModels.length;
	    			this.colIndex = 1;
    			} else if(sender.colIndex > 0) {
	    			//选择列
		    		if(this.curCell.colIndex <= sender.colIndex) {
		    		    this.selRect.beginColIndex = this.curCell.colIndex
		    		    this.selRect.endColIndex = sender.colIndex;
		    		} else {
		    			this.selRect.beginColIndex = sender.colIndex;
		    		    this.selRect.endColIndex = this.curCell.colIndex;
		    		}
		    		if(this.selRect.beginColIndex <= 0) {
		    			this.selRect.beginColIndex = 1;
		    		}
		    		this.colIndex = this.curCell.colIndex;		    		
	    		}
	    		var cNode = null;//this.dataSet.getCurNode();
	    		if(this.curCell != null) {
	    			cNode = this.curCell.parentNode.node;
	    			if(this.curCell.isBox === true) {
	    				cNode = this.curCell.node;
	    			}
	    		}
	    		
	    		var sNode = sender.parentNode.node;
	    		if(sender.isBox === true) {
    				sNode = sender.node;
    			}
	    		//选择行
	    		if(cNode != null && sNode != null) {
	    			if(cNode.getIndex() <= sNode.getIndex()) {
		    			this.selRect.beginRowNode = cNode;
		    		    this.selRect.endRowNode = sNode;
		    		} else {
		    			this.selRect.beginRowNode = sNode;
		    		    this.selRect.endRowNode = cNode;
		    		}
		    	}
	    		//this.post();
//	    		if(cNode != null) {
//	    		    this.dataSet.select(cNode);
//	    		}
	    		this.isCellMouseOver = true;
	    		this.refresh();
	    	}
	    },
	    doMouseDown:function(ent) {
	    	
	    },
	    /**
	     * 执行鼠标弹起事件.
	     * @param ent 事件对象
	     */
        doMouseUp:function(ent) {
        	this.titleMouseDown = false;
        	this.sortClick = false;
        	this.cellMouseDown = false;
        	this._checkBox = false;
        	//处理区域复制输入框的状态
	    	if(this.copyInput != null && this.dataSet.getRecordCount() > 0 && this.focus) {
		    	var cip = this.copyInput;
        		this._asynRun(function(){
        			if(this.cellMouseDown == false) {
	        			if(cip.offsetHeight > 0) {
	        			    cip.select(); 
	        			    cip.focus();
	        			}
        			}
	        		cip = null;
	        		that = null;
        		}, 10);
	    	}
	    },
	    /**
	     * 设置列宽度.
	     * @param titleCol 列对象， 
	     * @param width 宽度
	     * @param oldWidth 原宽度
	     */
	    _setColWidth:function(titleCol, oldWidth, nextCol, noldWidth, addWidth) {
	    	
	    	var colSpan = parseInt(titleCol.getAttribute("colSpan") || 1, 10);
	    	var avgWidth = parseInt(addWidth/colSpan, 10);
	        var i = 0;
	        var sumWidth = 0;
	        var realWidth = 0
			var k = 0;
			var tc = null;
			if(noldWidth - addWidth > 0) {
			    this._setNumberValue(nextCol, "width", noldWidth - addWidth);
			    //this.gridTable.rows[1].cells[1].innerHTML = this._colModels[nextCol.cellIndex].width + " " + addWidth;
			    this._colModels[nextCol.cellIndex - 1].width = this.nextOptionWidth - addWidth;
			    //this.gridTable.rows[1].cells[1].innerHTML += "=" + this._colModels[nextCol.cellIndex].width;
			} else {
				return;
			}
	        for(; i < colSpan - 1; i++) {
				tc = this.gridTable.rows[0].cells[titleCol.colIndex + i + k + 1];
				while(tc.style.display == "none") {
					k++;
					tc = this.gridTable.rows[0].cells[titleCol.colIndex + i + k + 1];
				}
				tc = null;
	        	//平均分配宽度
	        	sumWidth += avgWidth;
	        	var newWidth = oldWidth[i] + avgWidth;
	        	//调整单元格中的div宽
	        	var colIndex = titleCol.colIndex + i + k + 1;
	        	if(newWidth > 0) {
		    	    this._setNumberValue(this.gridTable.rows[0].cells[colIndex], "width", newWidth);
		    	    this._colModels[colIndex - 1].width = this.curOptionWidth[i] + avgWidth;
		    	    
		    	}	
	        	realWidth += newWidth - oldWidth[i];
	        }
			tc = this.gridTable.rows[0].cells[titleCol.colIndex + i + k + 1];
			while(tc.style.display == "none") {
				k++;
				tc = this.gridTable.rows[0].cells[titleCol.colIndex + i + k + 1];
			}
			tc = null;
	        //最后一列用总宽减去之前的宽的和
	        var newWidth = oldWidth[i] + (addWidth - sumWidth);
	        if(newWidth < 0) {
	        	return;
	        }
	        //调整输入控件
	        realWidth += newWidth - oldWidth[i];
	        //调整单元格中的div宽
	        var colIndex = titleCol.colIndex + i + k + 1;
	        if(newWidth > 0) {
	    	    this._setNumberValue(this.gridTable.rows[0].cells[colIndex], "width", newWidth);
	    	    this._colModels[colIndex - 1].width = this.curOptionWidth[i] + newWidth - oldWidth[i];
	    	}
	        this._autoHeight();
	    },
	    doMouseMove:function(ev) {
	    	this.mouseLeft = ev.clientX;
	    	if(this.titleMouseDown && this.titleDragState) {	    		
	    		var disX = this.mouseLeft - this.curMouseLeft;	
		        this._setColWidth(this.curTitleCell, this.curTitleCellWidth, this.nextTitleCell, this.nextTitleCellWidth, disX);	        
	    	}
	    	return false;
	    },
	    /**
	     * 初始化过滤窗口.
	     */
	    _initFilterWindow:function() {
	    	var _this = this;
	    	this.pfilter = new qzz.ui.drop.popfilter();
		    this.pfilter.setParent(_this.getBody());
		    this.pfilter.bind("onSort",function(fieldName, state){
		    	_this.setFocus();
		    	_this.setSort(fieldName, state);
			});
		    this.pfilter.bind("onClearFilter", function(fieldName){
		    	_this.setFocus();
		    	_this.setSort(fieldName, "none", false, function() {
            		_this.dataSet.clearFilterText(function(){
            			//隐藏其它列的属性
        		    	for(var key in _this.sortTables) {
        		    		var st = _this.sortTables[key];
        		    		if(st != null) {
        		    		    st.filtered = false
        		    		}
        		    		st = null;
        		    	}
        		    	_this.getOption().totalCount = _this.dataSet.getRecordCount();
						_this.pageObj.setTotalCount(_this.getOption().totalCount);
        				_this.refresh();
    	        		_this.trigger("onFiltered", [_this.dataSet]);
        			});
            	});
			});
		    this.pfilter.bind("onFilter", function(fieldName, totalCount, selItem){
		    	_this.setFocus();
	        	_this.setSort(fieldName, "none", false, function() {
	        		if(selItem != null && selItem.length < totalCount && selItem[0].id == "all") {
	        			selItem.splice(0, 1);
	        		}
	        		_this.dataSet.addFilterText(fieldName, selItem, function() {
	        			_this.getOption().totalCount = _this.dataSet.getRecordCount();
						_this.pageObj.setTotalCount(_this.getOption().totalCount);
	        			_this.refresh();
//	        			if(_this.getFieldName() !== fieldName) {
//	        			    _this.refreshSortState(fieldName);
//	        			    _this.refreshSortState(_this.getFieldName());
//	        			}
        	    	    _this.trigger("onFiltered", [_this.dataSet]);
	        		});
	        	});
			});
		    this.pfilter.bind("onCancel", function(){
		    	_this.setFocus();
		    });
        },
	    /**
	     * 执行排序事件.
	     * @param colIndex 列序号
	     * @param sortState 排序类型
	     */
	    doSortAndFilter:function(colIndex, sortState) {
	    	//var cm = this._colModels[colIndex - 1];
	    	var fieldName = this.getFieldName(colIndex); //cm.name;
	    	var cm = this.getColModel(fieldName);
	    	if(this.getOption().filter !== false) {
	    		//处理筛选状态	 
	    	    var _this = this;	 
	    	    if(this.inputFocus) {
	    	    	this.editObject.doBlur();
	    	    }
	    	    var tfi = this.dataSet.topFilterItem;
	    	    if(tfi == null || tfi.fieldName !== fieldName) {
			    	this.dataSet.collectFilter(fieldName, function() {
			    		_this._initFilterListBox(fieldName, 
			    				_this.dataSet.filterList[fieldName], 
			    				   _this.dataSet.selectFilterList);
			    	});
		    	} else {
		    		_this._initFilterListBox(fieldName, tfi.all, tfi.sel);
		    	}
	    	} else if(this.getOption().sort !== false){
		    	var _this = this;
		    	var ss = sortState;
		    	if(sortState == "none") {
		    		ss = "up";
		    	} else if(sortState == "up") {
		    		ss = "down";
		    	} else {
		    		ss = "none";
		    	}
		    	this.setSort(fieldName, ss);
	    	}	    	
	    },
	    /**
	     * 过滤窗口初始化.
	     * @param {string} fieldName 字段
	     * @param {json} 全部选项
	     * @param {json} 选择的选项 
	     */
	    _initFilterListBox:function(fieldName, filterList, selFilterList) {
    		if(this.pfilter.getFilterName() !== fieldName) {
    			this.pfilter.setFilterName(fieldName);
    			this.pfilter.clearFilter();
    		}
    		var oRect = this.sortTables[fieldName].getBoundingClientRect();
    		var left = oRect.left - 230;
    		if(left < 0) {
    			left = 0;
    		}
    		var scrrect = this.getScrollXY();
    		this.pfilter.execute(scrrect.top + oRect.top + 13, scrrect.left + left + 16, filterList, selFilterList);
	    },
	    hideCol:function(fieldNames) {
	    	if(typeof fieldNames == "string") {
	    		fieldNames = [fieldNames];
	    	}
	    	var rf = false;
	    	for(var i = 0, len = fieldNames.length; i < len; i++) {
	    		var cm = this.getColModel(fieldNames[i]);
	    		if(cm != null && cm.hidden === false) {
	    			cm.hidden = true;
	    			rf = true;
	    		}
	    	}
	    	if(rf) {
		    	this._initTitleSheet();
		    	this.refreshLayout();
	    	}
	    	return rf;
	    },
	    showCol:function(fieldNames) {
	    	if(typeof fieldNames == "string") {
	    		fieldNames = [fieldNames];
	    	}
	    	var rf = false;
	    	for(var i = 0, len = fieldNames.length; i < len; i++) {
	    		var cm = this.getColModel(fieldNames[i]);
	    		if(cm != null && cm.hidden !== false) {
	    			cm.hidden = false;
	    			rf = true;
	    		}
	    	}
	    	if(rf) {
		    	this._initTitleSheet();
		    	this.refreshLayout();
	    	}
	    	return rf;
	    },
	    /**
	     * 执行排序.
	     * @param {string} fieldName 字段
	     * @param {string} sortState 排序类型 up down none
	     * @param {boolean} doRefresh 对表格进行刷新
	     * @param {function} callBack 回调函数
	     */
	    setSort:function(fieldName, sortState, doRefresh, callBack) {	
	    	if(typeof doRefresh === "function") {	    		
	    		callBack = doRefresh;
	    		doRefresh = false;
	    	}
	    	var _this = this;
        	var sortTable = _this.sortTables[fieldName];
        	if(sortTable == null || sortTable.sortState == sortState) {
        		if(typeof callBack === "function") {
	    			callBack.apply(_this, arguments)
	    		}
        	} else {
	        	sortTable.sortState = sortState;
	        	if(_this.hasbind("onSort")) {
	        		_this._sortFieldName = fieldName;
	        	    _this.trigger("onSort", [fieldName, sortState]);
	        	} else {
			    	_this.dataSet.doSort(fieldName, sortState, function(){	
			    		//_this.setFocus();
			    		_this._sortFieldName = fieldName;
			    		if(doRefresh != false) {
			    		    _this.refresh();
			    		} else {
			    			_this.dataSet.first();
			    		}
			    		if(typeof callBack === "function") {
			    			callBack.apply(_this, arguments)
			    		}
			    	});		    		
			    	_this.trigger("onSortCol",[_this.colIndex - 1, _this.colIndex, _this.state]);
	        	}
        	}
        },
	    /**
	     * 设置复制数据.
	     * @return 返回字符串
	     */
	    getCopyData:function() {
	    	//是否区域选择
	    	if(this.mulCellSelect) {
	    		//this.dataSet 是表格控件的数据模型
	    		//缓存当前选择行
	    		var tmpNode = this.dataSet.getCurNode();
	    		//添加beginUpdate, endUpdate禁止获取数据过程中不刷新表格
	    		this.dataSet.beginUpdate();
	    		//区域选择的起始行设置为选择行
	    		this.dataSet.select(this.selRect.beginRowNode);
	    		//复制的数据内容数组
	    		var copyStr = [];
	    		//获取列模型
	    		var colModel = this.option["colModels"];
	    		var ri = 0;
	    		//从起始行到结束行循环数据模型，
	    		while (!this.dataSet.isEof() && this.dataSet.getIndex() <= this.selRect.endRowNode.getIndex()) {
	    			var rowStr = [];
	    			//从起始列到结束列循环数据模型
	    			for(var j = this.selRect.beginColIndex,ci = 0; j <= this.selRect.endColIndex; j++) {
	    				//获取数据模型
	    				var cm = this._colModels[j - 1];
	    				//判断是否是显示列
	    				if(cm.name != null && cm.hidden != true) {
	    					//获取单元格显示的数据
		    				var cstr = this.dataSet.getDisplay(cm.name);
		    				//对特殊标签或字符进行转换
		    				if(typeof cstr === "string") {
		    					cstr = cstr.replace(/&gt;|&lt;/g, function(key){
			    					if(key === "&gt;") {
			    						return ">";
			    					} else {
			    						return "<";
			    					}
			    				});
		    				}
		    				//添加到列字符串数组中
		    				rowStr[ci++] = cstr === "" ? " ":cstr;
	    				}
	    			}
	    			//添加到行字符串数组中
	    			copyStr[ri++] = rowStr.join("\t");
	    			this.dataSet.next();
	    		}
	    		//还原选择行
	    		this.dataSet.select(tmpNode);
	    		//结束刷新控制
	    		this.dataSet.endUpdate(false);
	    		//把结果数组转换成字符串返回
	    		return copyStr.join("\n");
	    		tmpNode = null;
	    	} else {
	    		return this.dataSet.getDisplay(this.getFieldName());
	    	}
	    },
	    /**
	     * 开始刷新
	     */
	    beginUpdate:function() {
	    	this.base();
	    	this.dataSet.beginUpdate();
	    },
	    /**
	     * 结束刷新
	     */
	    endUpdate:function(state) {
	    	this.dataSet.endUpdate(state);
	    	this.base();	    	
	    },
	    setTotalCount:function(value) {
	    	this.getOption().totalCount = parseInt(value, 10);
			this.pageObj.setTotalCount(this.getOption().totalCount);
	    }
	});
	/**
	 * 列表表格控件.
	 */
	_bindRenderFunc("qzzlistgrid", qzz.ui.listgrid);
	return qzz.ui.listgrid;
});
	